﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AppSocketModel;
using AppSocketService.Account;
using BCGPSCommon;
using BCGPSRESTful.WebApiConfig;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BCGPSRESTful.Controllers.Page.Account.SystemAccount
{
    /// <summary>
    /// 系统账户
    /// </summary> 
    [ApiController]
    [Route("api/[controller]")]
    [ApiExplorerSettings(GroupName = "SystemAccount")]
    public class SystemAccountController : PageBaseController
    {
        private readonly TimeSpan expiration = TimeSpan.FromDays(7);
        private readonly ISystemAccountService  systemAccountService;
        private readonly IAccountService accountService;
        private readonly IAccountPersonService accountPersonService;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="systemAccountService"></param>
        /// <param name="accountService"></param>
        /// <param name="accountPersonService"></param>
        public SystemAccountController(ISystemAccountService systemAccountService, IAccountService accountService,IAccountPersonService  accountPersonService )
        {
            this.systemAccountService = systemAccountService;
            this.accountService = accountService;
            this.accountPersonService = accountPersonService;
        }
        
        #region 注册系统账户
        /// <summary>
        /// 注册系统账户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [AllowAnonymous]
        public ActionResult<SystemAccountDto> Register(SystemAccountRegisterDto dto)
        {
            return systemAccountService.RegisterSystemAccount(dto);
        }
        #endregion

        #region 账户登录
        /// <summary>
        /// 账户登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [AllowAnonymous]
        [ProducesResponseType(typeof(TokenDto), 200)]
        public ActionResult<TokenDto> Login([FromBody] SystemAccountLoginDto dto)
        {
            var ip = Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrWhiteSpace(ip))
                ip = HttpContext.Connection.RemoteIpAddress.ToString();

            var accountDto = systemAccountService.Login(dto, ip);

            if (accountDto == null)
                throw new ArgumentException("用户名或密码不正确!");

            if (accountDto.accountRole != AccountRole.SYSTEM_PAY)
                accountDto.accountRole = AccountRole.SYSTEM;
            return new TokenDto
            {
                Description = "WELCAME:" + accountDto.SystemAccountCode,
                AccessToken = accountDto.GenerateToken(expiration, accountDto.accountRole),
                ExpiresIn = (int)expiration.TotalSeconds,
                EndTime = DateTime.Now.AddSeconds(expiration.TotalSeconds - 12 * 3600)
            };
        }

        #endregion


        #region 获取全部账户  -- 分页
        /// <summary>
        /// 获取全部用户 -- 分页
        /// </summary>
        /// <param name="companyId">公司ID</param>
        /// <param name="accountCode">账户号</param>
        /// <param name="accountRole">账户角色</param>
        /// <param name="accountCodeType">账户类型</param>
        /// <param name="index">页码</param>
        /// <param name="size">记录数</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<AccountDto>), 200)]
        public PageableList<AccountDto> QueryAccountPage(
            [FromQuery (Name = "company_id")]int? companyId,
            [FromQuery(Name = "account_code")]string accountCode,
            [FromQuery(Name = "account_role")]AccountRole? accountRole,
            [FromQuery(Name = "account_code_type")]AccountCodeType? accountCodeType,
            [FromQuery(Name = "index")]int? index=0,
            [FromQuery(Name = "size")]int? size=20)
        {
            if(accountRole.HasValue)
            {
                if(accountRole.Value == AccountRole.PERSON)
                {
                    return accountPersonService.SystemGetAll(accountCode, accountRole, accountCodeType, false, index.Value, size.Value);
                }
            }
            return accountService.SystemGetAll(companyId,accountCode, accountRole, accountCodeType,false,index.Value,size.Value);
        }
        #endregion
    }
}